<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				exists:[],
				fileId:"",
				orderNumCache:"work-monthlyworkadjustapply-orderNum-cache",
				adjustTime:''
			})
		},
		methods:{
			getAdjustTime(time){
				let _that = this
				let startTime = moment(time).add(1,'M').startOf('month').format("YYYY-MM-DD")
				_that.adjustTime = startTime
				_that.$refs.autoform.changeEditor("workInfo","adjustPlanEnd","min",startTime)
			},
			initLoad(edit){
				let _that = this
				_that.initAdjustSelect()
				_that.initAdjustReason()
				_that.$nextTick(function(){
					 if(edit === "true"){
						let obj = {
							code :_that.formdata.adjustTypeCode
						}
						_that.onAdjustType(obj)
					 }
				})
			},
			initAdjustSelect(){
				this.om.request(
					'dictZero/getListByCode',
					{
						code:"37768"
					}
				).then(res =>{
					this.$refs.autoform.changeEditor('workInfo','adjustTypeCode','data',configSelectData(res,'code','dicname'))
					this.$refs.autoform.initSelectIndex('workInfo','adjustTypeCode')
				})
			},
			initAdjustReason(){
				this.om.request(
					'dictZero/getListByCode',
					{
						code:"37780"
					}
				).then(res =>{
					this.$refs.autoform.changeEditor('workInfo','adjustReasonCode','data',configSelectData(res,'code','dicname'))
					this.$refs.autoform.initSelectIndex('workInfo','adjustReasonCode')
				})
			},
			onAdjustType(item){
				let _that =  this
				if(item.code !== "37769"){
					_that.$refs.autoform.changeEditor('workInfo',"adjustPlanEnd",'type','')
					_that.$refs.autoform.changeEditor('workInfo',"adjustPlanEnd",'required',false)
				}else{
					_that.$refs.autoform.changeEditor('workInfo',"adjustPlanEnd",'type','datebox')
					_that.$refs.autoform.changeEditor('workInfo',"adjustPlanEnd",'required',true)
					_that.$refs.autoform.formdata.adjustPlanEnd = _that.adjustTime 
				}
				this.initLoad()
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				_that.exists = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.listId).join(",")
				if(e.edit == "true"){
					_that.fileId = e.formId
				}
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor('workInfo',"adjustPlanEnd",'required',true,_that.autoformdata)
							changeEditor(_that.autoformdata[0].editors,"orderNum","link","../../public/pageSearch/pageSearch")
							changeEditor(_that.autoformdata[0].editors,'orderNum',"linkParam",{
									other: JSON.stringify({
										sList:_that.exists,
										editId:_that.fileId?_that.fileId:''
									}),
									namefield:'orderNumStr',
									notefield:'workName',
									searchfield:"q",
									initload:true,
									url:'tmonthlyWorkAdjustApply/getOrderNumList',
									cachename:_that.orderNumCache,
									placeholder:"请输入代码或名称"
								})
								_that.initLoad()
							}
						}
					})
				}
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.origPlanEnd){
						_that.getAdjustTime(_that.$refs.autoform.formdata.origPlanEnd)
					}
				})		
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.orderNumCache,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.listId = d.id
							formdata.orderNum = d.orderNumStr
							formdata.workName = d.workName
							formdata.deliveryName = d.deliveryName
							formdata.origPlanEnd = d.planEnd
							formdata.evaluateUserName = d.evaluateUserName
							_that.initAdjustSelect()
							_that.initAdjustReason()
							_that.getAdjustTime(d.planEnd)
							uni.removeStorage({
								key:_that.orderNumCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                     
